package com.cowmooed.utils;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 用户上下文工具类
 * 用于获取当前登录用户信息
 */
public class UserContextUtil {

    /**
     * 获取当前登录用户名
     */
    public static String getCurrentUsername() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attributes != null) {
            HttpServletRequest request = attributes.getRequest();
            return (String) request.getAttribute("currentUser");
        }
        return null;
    }

    /**
     * 判断用户是否已登录
     */
    public static boolean isAuthenticated() {
        return getCurrentUsername() != null;
    }
}